<?php


class connector{
	
	var $DB_SERVER = 'localhost';
	var $DB_NAME = 'formacio';
	var $DB_USER = 'root';
	var $DB_PASS = 'corredera';
	
	function conector(){
		$mysqli = new mysqli($this->DB_SERVER,$this->DB_USER,$this->DB_PASS,$this->DB_NAME);
		if ($mysqli->connect_errno) {
			echo "Fallo al contenctar a MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
		}
		$mysqli->set_charset("utf8");
		return $mysqli;
	}
	
	function query($query){
		$mysqli = $this->conector();
		$mysqli->query($query);
		$result = $mysqli->query($query);
		
		while($row = $result->fetch_assoc()) {
			$rows[] = $row;
		}
		$mysqli->close();
		return $rows;
	}
	
	function query_one_row($query){
		$mysqli = $this->conector();
		$mysqli->query($query);
		$result = $mysqli->query($query);
		$mysqli->close();
		return $result->fetch_object();
	}
	
	function query_row($query){
		$mysqli = $this->conector();
		$mysqli->query($query);
		$result = $mysqli->query($query);
		
		while ($obj = $result->fetch_object()) {
			$resultObject = new stdClass();
			
			foreach ($obj as $key => $value) {
				$resultObject->$key = $value;
			}
			$rows[] = $resultObject;
		}
		$mysqli->close();
		if(!empty($rows)){
			return $rows;
		}else{
			return null;
		}
	   
	}
	
	function execute($query){
		$mysqli = $this->conector();
		$mysqli->query($query);
		if($mysqli->error == null){
			return true;
		}else if($mysqli->error != null){
			error_log("\r\n\r\nError Execute method ".date("F j, g:i a").":\r\n".$query."\r\n".$mysqli->error, 3, 'logs.txt');
			//$mysqli->error añadir a log para ver que falla
			return false;
		}
	}
	
	
}



?>